package net.starwifi.duobao.web.db;


import net.starwifi.duobao.entity.DBOrder;
import net.starwifi.duobao.entity.DBTimes;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2015/12/1.
 */
@Controller("apiAdminRMIController")
@RequestMapping("/rmi")
public class AdminRMIController extends BaseController {

    private static final String PASS="jiaChuang123RMI";

    @Autowired
    StaticService staticService;

    @Autowired
    DBTimesService dbTimesService;

    @Autowired
    DBOrderService dbOrderService;

    @Autowired
    SettingService settingService;

    @Resource(name = "freeMarkerConfigurer")
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @ResponseBody
    @RequestMapping(value = "/api",method = RequestMethod.GET)
    public Object cache(String key,String content,String pass){
        if(!pass.equals(PASS)){
            return Message.error("pass error");
        }
        if(key.equals("pageCache")){  //clear the freeMarker page cache.
            freeMarkerConfigurer.getConfiguration().clearTemplateCache();
            return Message.success("success");
        }
        if(key.equals("memCache")){ //clear mem cache
            return Message.success("success");
        }
        if(key.equals("buildDBIndex")){ //build the /dbs/index.html page
            buildDBIndex();
            return Message.success("");
        }
        if(key.equals("buildDBTimes")){
            int count=buildDBTimesByTimesNo(content);
            return Message.success(count+"");
        }
        if(key.equals("buildDBProduct")){
            int count=buildDBProduct(Long.parseLong(content));
            return Message.success(count+"");
        }
        if(key.equals("dbOrder")){
            DBOrder dbOrder=dbOrderService.find(Long.parseLong(content));
            if(dbOrder!=null){
                dbOrderService.payment(dbOrder,null);
            }
        }
        if(key.equals("findLottery")){
            DBTimes dbTimes=dbTimesService.findByTimesNo(content);
            if(dbTimes!=null&&!dbTimes.getIsLottery()){
                dbTimesService.findLottery(dbTimes);
            }
            return Message.success("call success");
        }
        if(key.equals("clearCache")){
            settingService.clearCache();
        }
        return Message.error("nothings to do");
    }


    /**
     * 一元夺宝产品页
     */
    @ResponseBody
    @RequestMapping(value = "/static",method = RequestMethod.GET)
    public Object db(String key,String content,String pass) {
        if(!pass.equals(PASS)){
            return Message.error("pass error");
        }
        if(key.equals("dbIndex")){
             int count= buildDBIndex();
            return Message.success(count+"");
        }
        if(key.equals("dbTimes")){
            int count= buildDBTimes(content);
            return Message.success(count+"");
        }
        return Message.success("nothings to do");
    }


    @ResponseBody
    @RequestMapping(value = "/dbCode",method = RequestMethod.GET)
    public Object autoCode() {
        dbOrderService.autoOrderCodeInfo();
        return Message.success("success");
    }



    public int buildDBIndex(){
        return staticService.buildDBIndex();
    }

    public int buildDBTimes(String content){
        DBTimes dbTimes=dbTimesService.find(Long.parseLong(content));
        return staticService.buildDBTimes(dbTimes);
    }

    public int buildDBProduct(Long productId){
        List<DBTimes> dbTimesList=dbTimesService.getListData("o.dbProduct.id=?1",new Object[]{productId});
        int count=0;
        for(DBTimes dbTimes:dbTimesList){
            staticService.buildDBTimes(dbTimes);
            count=count+1;
        }
        return count;
    }

    public int buildDBTimesByTimesNo(String content){
        DBTimes dbTimes=dbTimesService.findByTimesNo(content);
        return staticService.buildDBTimes(dbTimes);
    }

}
